"""
五子棋主菜单界面
"""

import pygame
import sys


class MenuGUI:
    """主菜单界面类"""
    
    def __init__(self, width=600, height=500):
        """
        初始化菜单界面
        :param width: 窗口宽度
        :param height: 窗口高度
        """
        self.width = width
        self.height = height
        
        # 初始化Pygame
        pygame.init()
        self.screen = pygame.display.set_mode((self.width, self.height))
        pygame.display.set_caption("五子棋 - 主菜单")
        self.clock = pygame.time.Clock()
        
        # 颜色定义
        self.COLOR_BG = (240, 240, 240)
        self.COLOR_TITLE = (50, 50, 50)
        self.COLOR_BUTTON = (100, 150, 200)
        self.COLOR_BUTTON_HOVER = (120, 170, 220)
        self.COLOR_BUTTON_TEXT = (255, 255, 255)
        self.COLOR_BORDER = (80, 80, 80)
        
        # 字体
        try:
            self.title_font = pygame.font.SysFont('microsoftyahei', 56, bold=True)
            self.button_font = pygame.font.SysFont('microsoftyahei', 28)
            self.desc_font = pygame.font.SysFont('microsoftyahei', 18)
        except:
            try:
                self.title_font = pygame.font.SysFont('simhei', 56, bold=True)
                self.button_font = pygame.font.SysFont('simhei', 28)
                self.desc_font = pygame.font.SysFont('simhei', 18)
            except:
                self.title_font = pygame.font.SysFont('arial', 56, bold=True)
                self.button_font = pygame.font.SysFont('arial', 28)
                self.desc_font = pygame.font.SysFont('arial', 18)
        
        # 创建菜单按钮
        self.buttons = self.create_buttons()
        self.selected_mode = None
        
    def create_buttons(self):
        """创建菜单按钮"""
        button_width = 400
        button_height = 60
        button_x = (self.width - button_width) // 2
        start_y = 150
        spacing = 80
        
        buttons = {
            'pvp': {
                'rect': pygame.Rect(button_x, start_y, button_width, button_height),
                'text': '双人对战',
                'desc': '玩家 vs 玩家'
            },
            'random': {
                'rect': pygame.Rect(button_x, start_y + spacing, button_width, button_height),
                'text': '随机AI对战',
                'desc': '适合新手练习'
            },
            'greedy': {
                'rect': pygame.Rect(button_x, start_y + spacing * 2, button_width, button_height),
                'text': '贪心AI对战',
                'desc': '中等难度挑战'
            },
            'minimax': {
                'rect': pygame.Rect(button_x, start_y + spacing * 3, button_width, button_height),
                'text': 'Minimax AI对战',
                'desc': '高级AI对手'
            }
        }
        return buttons
    
    def draw_menu(self):
        """绘制菜单界面"""
        # 填充背景
        self.screen.fill(self.COLOR_BG)
        
        # 绘制标题
        title_text = "五子棋"
        title_surface = self.title_font.render(title_text, True, self.COLOR_TITLE)
        title_rect = title_surface.get_rect(center=(self.width // 2, 80))
        self.screen.blit(title_surface, title_rect)
        
        # 绘制按钮
        mouse_pos = pygame.mouse.get_pos()
        
        for mode, button_info in self.buttons.items():
            rect = button_info['rect']
            
            # 检查鼠标悬停
            is_hover = rect.collidepoint(mouse_pos)
            color = self.COLOR_BUTTON_HOVER if is_hover else self.COLOR_BUTTON
            
            # 绘制按钮背景
            pygame.draw.rect(self.screen, color, rect, border_radius=10)
            pygame.draw.rect(self.screen, self.COLOR_BORDER, rect, 3, border_radius=10)
            
            # 绘制按钮文字
            text_surface = self.button_font.render(button_info['text'], True, self.COLOR_BUTTON_TEXT)
            text_rect = text_surface.get_rect(center=(rect.centerx, rect.centery - 8))
            self.screen.blit(text_surface, text_rect)
            
            # 绘制描述文字
            desc_surface = self.desc_font.render(button_info['desc'], True, self.COLOR_BUTTON_TEXT)
            desc_rect = desc_surface.get_rect(center=(rect.centerx, rect.centery + 15))
            self.screen.blit(desc_surface, desc_rect)
    
    def handle_click(self, pos):
        """
        处理鼠标点击事件
        :param pos: 鼠标位置
        :return: 选择的模式 ('pvp', 'random', 'greedy', 'minimax') 或 None
        """
        for mode, button_info in self.buttons.items():
            if button_info['rect'].collidepoint(pos):
                return mode
        return None
    
    def run(self):
        """运行菜单主循环"""
        running = True
        
        while running:
            # 处理事件
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
                elif event.type == pygame.MOUSEBUTTONDOWN:
                    if event.button == 1:  # 左键点击
                        mode = self.handle_click(event.pos)
                        if mode:
                            self.selected_mode = mode
                            return mode
            
            # 绘制界面
            self.draw_menu()
            pygame.display.flip()
            self.clock.tick(60)
        
        return None
